package com.yiwei.zhh.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.zhh.R;
import com.yiwei.zhh.adapter.AlipayAddListAdapter;
import com.yiwei.zhh.api.callback.AlipayOperatorContract;
import com.yiwei.zhh.api.requestInfo.AlipayRequest;
import com.yiwei.zhh.event.AlipayUserEvent;
import com.yiwei.zhh.model.AlipayListModel;
import com.yiwei.zhh.util.UI;
import com.yiwei.zhh.view.Dialog;
import com.yiwei.zhh.view.ListItemView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/8/25.
 */

public class V3AlipayAddActivity extends BaseActivity implements AlipayOperatorContract.View {

    @BindView(R.id.left_img)
    ImageButton leftImg;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.btn_delete)
    ImageView btnDelete;
    @BindView(R.id.list)
    ListView list;
    @BindView(R.id.view_has_data)
    View viewHasData;
    @BindView(R.id.btn_add)
    SuperButton btnAdd;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private Unbinder unbiner;
    private AlipayAddListAdapter adapter;
    private AlipayRequest api;
    private android.app.Dialog dialog;
    private List<AlipayListModel.DataBean> totalData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alipay_add_v3);
        initBar();
        unbiner = ButterKnife.bind(this);
        String alipay = getIntent().getStringExtra("alipay");
        if (TextUtils.isEmpty(alipay)) {
            alipay = "";
        }
//        adapter = new AlipayUserV3Adapter(this, alipay);
        adapter = new AlipayAddListAdapter(this);
        adapter.setonAlipayClickListener(new AlipayAddListAdapter.OnAlipayClick() {
            @Override
            public void onClick(String account, int poi) {
//                adapter.isClick = true;
//                adapter.selectPos = poi;
                adapter.notifyDataSetChanged();
                EventBus.getDefault().post(new AlipayUserEvent(account));
                finish();
            }

            @Override
            public void delete(String account) {
                api.operatorUser(api.DELETE, account);
            }
        });
        list.setAdapter(adapter);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                api.getAlipayList();
            }
        });
        api = AlipayRequest.create();
        api.attachView(this);
        api.getAlipayList();


        dialog = Dialog.showAddAlipayV3(this, new Dialog.AlipayClickListener() {
            @Override
            public void confirm(String user, android.app.Dialog dialog) {
                api.operatorUser(api.CREATE, user);
            }

            @Override
            public void cancel() {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    private boolean isEdit = true;

    @OnClick({R.id.left_img, R.id.btn_delete, R.id.btn_add})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_delete:
                if (totalData == null || totalData.size() <= 3) {
                    dialog.show();
                }
                break;
            case R.id.left_img:
                finish();
                break;
            case R.id.btn_add:
                if (totalData == null || totalData.size() <= 3) {
                    dialog.show();
                }

                break;
        }
    }


    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        if (refresh != null) {
            refresh.setRefreshing(false);
        }

    }

    @Override
    public void onRequestStart() {
        if (refresh != null) {
            refresh.setRefreshing(true);
        }
    }

    @Override
    public void getAlipayListSuccess(AlipayListModel item) {
        totalData = item.data;
        adapter.updateData(item.data);
        if (item.data != null && item.data.size() != 0) {
            viewHasData.setVisibility(View.GONE);
            layoutEmpty.setVisibility(View.GONE);
            if (item.data.size() >= 3) {
                btnDelete.setImageResource(R.mipmap.img_alipay_add_fail);
            } else {
                btnDelete.setImageResource(R.mipmap.img_alipay_add_success);
            }
        } else {
            viewHasData.setVisibility(View.VISIBLE);
            layoutEmpty.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void operatorUserSuccess() {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        api.getAlipayList();
    }
}
